//
//  OrderViewCell.m
//  EastMarket
//
//  Created by 李鹏程 on 16/10/13.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "OrderViewCell.h"

@implementation OrderViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        CGFloat spacing = 8.0f;
        CGFloat border = 1.0f;
        CGFloat lblHeight = 44.0f;
        UILabel *lblHeadLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, border * 2)];
        lblHeadLine.backgroundColor = EastMarket_MainToneColor;
        [self.contentView addSubview:lblHeadLine];
        
        //
        UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, lblHeadLine.frame.origin.y + lblHeadLine.frame.size.height, SCREEN_WIDTH, lblHeight)];
        viewHeader.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:viewHeader];
        _lblOrderID = [[UILabel alloc] initWithFrame:CGRectMake(spacing, 0, (viewHeader.frame.size.width - border * 2 - 2 * spacing) * 3 / 5, viewHeader.frame.size.height)];
        [_lblOrderID setFont:[UIFont systemFontOfSize:17]];
        [_lblOrderID setTextColor:[UIColor blackColor]];
        [viewHeader addSubview:_lblOrderID];
        
        _btnOrderState = [[UIButton alloc] initWithFrame:CGRectMake(_lblOrderID.frame.origin.x + _lblOrderID.frame.size.width + border, _lblOrderID.frame.origin.y, viewHeader.frame.size.width - _lblOrderID.frame.origin.x - _lblOrderID.frame.size.width - 2 * border - 16 - spacing - 8, _lblOrderID.frame.size.height)];
        [_btnOrderState setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        _btnOrderState.titleLabel.font = [UIFont systemFontOfSize:16];
        [_btnOrderState setTitleColor:EastMarket_darkGreyFontColor forState:UIControlStateNormal];
        [_btnOrderState addTarget:self action:@selector(btnStateOperateClick) forControlEvents:UIControlEventTouchUpInside];
        [viewHeader addSubview:_btnOrderState];
        
        _imgArr = [[UIImageView alloc] initWithFrame:CGRectMake(viewHeader.frame.size.width - spacing - 16 - 8, viewHeader.frame.size.height / 2 - 8, 16, 16)];
        _imgArr.image = [UIImage imageNamed:@"rightArrow"];
        [viewHeader addSubview:_imgArr];
        
        //订单名称
        _lblOrderName = [[UILabel alloc] initWithFrame:CGRectMake(0, viewHeader.frame.origin.y + viewHeader.frame.size.height + border, SCREEN_WIDTH, lblHeight)];
        _lblOrderName.backgroundColor = EastMatket_OrderNameBackgroundColor;
        _lblOrderName.textColor = EastMarket_darkBlackFontColor;
        _lblOrderName.font = [UIFont systemFontOfSize:14];
//        [self.contentView addSubview:_lblOrderName];
//        _lblOrderName = [[UILabel alloc] initWithFrame:CGRectMake(spacing, 0, viewOrderName.frame.size.width - 2 * spacing, _lblOrderID.frame.size.height)];
//        [viewOrderName addSubview:_lblOrderName];
        
        UICollectionViewFlowLayout *layoutOfProMainCate = [UICollectionViewFlowLayout new];
//        layoutOfProMainCate.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 44);
        layoutOfProMainCate.itemSize = CGSizeMake((SCREEN_WIDTH - 32)  /  2, 44.0f);//每个cell的大小
        layoutOfProMainCate.minimumLineSpacing = 0.0f;//每行的最小间距
        layoutOfProMainCate.minimumInteritemSpacing = 0.0f;//每列的最小间距
        layoutOfProMainCate.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//网格视图的/上/左/下/右,的边距
        
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, viewHeader.frame.origin.y + viewHeader.frame.size.height + border, SCREEN_WIDTH, lblHeight) collectionViewLayout:layoutOfProMainCate];
        _collectionView.backgroundColor = EastMatket_OrderNameBackgroundColor;
        [_collectionView registerClass:[OrderCollectionViewCell class] forCellWithReuseIdentifier:@"ProductName"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
//        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.showsVerticalScrollIndicator = false;
        _collectionView.scrollEnabled = false;

        [self.contentView addSubview:_collectionView];
//        [_collectionView reloadData];
//        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewTitle"];

        
        // 数量总额
        _viewOrderNumAndSum = [[UIView alloc] initWithFrame:CGRectMake(0, _collectionView.frame.origin.y + _collectionView.frame.size.height + border, SCREEN_WIDTH, lblHeight)];
        _viewOrderNumAndSum.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_viewOrderNumAndSum];
        _lblOrderNum = [[UILabel alloc] initWithFrame:CGRectMake(spacing, 0, (_viewOrderNumAndSum.frame.size.width - border * 2 - 2 * spacing) / 2, _viewOrderNumAndSum.frame.size.height)];
        _lblOrderNum.font = [UIFont systemFontOfSize:14];
        [_viewOrderNumAndSum addSubview:_lblOrderNum];
        UILabel *lblSumTips = [[UILabel alloc] initWithFrame:CGRectMake(_lblOrderNum.frame.origin.x + _lblOrderNum.frame.size.width + border, _lblOrderNum.frame.origin.y, (_viewOrderNumAndSum.frame.size.width - border * 2 - 2 * spacing) / 4, _lblOrderNum.frame.size.height)];
        lblSumTips.text = @"总金额:";
        lblSumTips.font = [UIFont systemFontOfSize:14];
        lblSumTips.textAlignment = NSTextAlignmentRight;
        [_viewOrderNumAndSum addSubview:lblSumTips];
        _lblOrderTotalPrice = [[UILabel alloc] initWithFrame:CGRectMake(lblSumTips.frame.origin.x + lblSumTips.frame.size.width + border, _lblOrderNum.frame.origin.y, (_viewOrderNumAndSum.frame.size.width - border * 2 - 2 * spacing) / 4, _lblOrderNum.frame.size.height)];
        _lblOrderTotalPrice.textColor = EastMarket_MainToneColor;
        _lblOrderTotalPrice.font = [UIFont systemFontOfSize:14];
        [_viewOrderNumAndSum addSubview:_lblOrderTotalPrice];
        
        // AddTime & PayBtn
        _viewAddTimeAndPay = [[UIView alloc] initWithFrame:CGRectMake(0, _viewOrderNumAndSum.frame.origin.y + _viewOrderNumAndSum.frame.size.height + border, SCREEN_WIDTH, lblHeight)];
        _viewAddTimeAndPay.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_viewAddTimeAndPay];
        _lblOrderAddTime = [[UILabel alloc] initWithFrame:CGRectMake(spacing, 0, (_viewAddTimeAndPay.frame.size.width - border - 2 * spacing) * 3 / 4, _viewAddTimeAndPay.frame.size.height)];
        _lblOrderAddTime.textColor = EastMarket_darkGreyFontColor;
        _lblOrderAddTime.font = [UIFont systemFontOfSize:14];
        [_viewAddTimeAndPay addSubview:_lblOrderAddTime];
        _lblOrderAddTime.backgroundColor = [UIColor clearColor];
        
        _btnOrderPay = [[UIButton alloc] initWithFrame:CGRectMake(_lblOrderAddTime.frame.origin.x + _lblOrderAddTime.frame.size.width + border, 8, (_viewAddTimeAndPay.frame.size.width - border - 2 * spacing) / 4 - 8 - 8, _lblOrderAddTime.frame.size.height - 16)];
        _btnOrderPay.backgroundColor = [UIColor clearColor];
        _btnOrderPay.titleLabel.font = [UIFont systemFontOfSize:14];
        [_btnOrderPay setTitleColor:EastMarket_MainToneColor forState:UIControlStateNormal];
        [_btnOrderPay addTarget:self action:@selector(btnPayOperateClick) forControlEvents:UIControlEventTouchUpInside];
        _btnOrderPay.layer.cornerRadius = 5.0f;
        _btnOrderPay.layer.borderWidth = border;
        _btnOrderPay.layer.borderColor = [EastMarket_MainToneColor CGColor];
        [_viewAddTimeAndPay addSubview:_btnOrderPay];
        
        _viewSpacing = [[UIView alloc] initWithFrame:CGRectMake(0, _viewAddTimeAndPay.frame.origin.y + _viewAddTimeAndPay.frame.size.height, SCREEN_WIDTH, 12)];
        [self.contentView addSubview:_viewSpacing];
        _viewSpacing.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate {
    _collectionView.dataSource = dataSourceDelegate;
    _collectionView.delegate = dataSourceDelegate;
    [_collectionView reloadData];
}

#pragma mark -- UICollectionView delegate * darasource
// 定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _productNameArr.count;
    debugLog(@"lulululululu%@", _productNameArr);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifyCate = @"ProductName";
    OrderCollectionViewCell *cellCate = [collectionView dequeueReusableCellWithReuseIdentifier:identifyCate forIndexPath:indexPath];
    [cellCate sizeToFit];
    NSInteger curRow = [indexPath row];
    cellCate.backgroundColor = EastMatket_OrderNameBackgroundColor;
    NSDictionary *orderDic = [_productNameArr objectAtIndex:curRow];
    cellCate.lblProductName.text = [NSString stringWithFormat:@"%@",[[orderDic objectForKey:@"item"] objectForKey:@"title"]];
    
    return cellCate;
}

- (void)updateUI {

    _collectionView.frame = CGRectMake(_collectionView.frame.origin.x, _collectionView.frame.origin.y, _collectionView.frame.size.width, (_productNameArr.count + 1) / 2 * 44);
    _viewOrderNumAndSum.frame = CGRectMake(_viewOrderNumAndSum.frame.origin.x, _viewOrderNumAndSum.frame.origin.y, _viewOrderNumAndSum.frame.size.width, _viewOrderNumAndSum.frame.size.height);
    _viewAddTimeAndPay.frame = CGRectMake(_viewAddTimeAndPay.frame.origin.x, _viewAddTimeAndPay.frame.origin.y, _viewAddTimeAndPay.frame.size.width, _viewAddTimeAndPay.frame.size.height);
    _viewSpacing.frame = CGRectMake(_viewSpacing.frame.origin.x, _viewSpacing.frame.origin.y, _viewSpacing.frame.size.width, _viewSpacing.frame.size.height);
}

- (void)btnStateOperateClick
{
    if (self.btnOrderStateClick) {
        self.btnOrderStateClick();
    }
}

- (void)btnPayOperateClick {
    
    if (self.btnOrderPayClick) {
        self.btnOrderPayClick();
    }
}
@end
